package com.eight.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.travel.dao.PlaceOrderMapper;
import com.eight.travel.pojo.Place;
import com.eight.travel.pojo.PlaceOrder;
import com.eight.travel.pojo.Placecom;
import com.eight.travel.pojo.Sign;
import com.eight.travel.utils.AliPay;
import com.eight.travel.utils.BaseResp;
import com.eight.travel.service.PlaceService;
import com.eight.travel.utils.UploadPic;
import freemarker.template.utility.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;


@RestController
@Api(value = "景点模块")
@RequestMapping("/place")
public class PlaceController {

    @Autowired
    private PlaceService placeService;
    @Resource
    private PlaceOrderMapper placeOrderMapper;

    @ApiOperation(value = "分页查询全部景区信息",notes = "需传入 当前页page和显示行数size")
    @GetMapping("/findAll/{page}/{size}")
    public BaseResp findAll(@PathVariable("page")int page,@PathVariable("size") int size){
        BaseResp places= placeService.findAll(page,size);
        return places;
    }

    @ApiOperation(value = "增加点击率",notes = "需传入 景点pid和点击率click")
    @GetMapping("/click")
    public BaseResp click(int id,int click){
        BaseResp baseResp = placeService.updateClick(id, click);
        return baseResp;
    }

    @ApiOperation(value = "根据pid查询用户结果",notes = "需传入 景点pid")
    @GetMapping("/findOne")
    public BaseResp findOne(int id){
        BaseResp baseResp = placeService.findOne(id);
        return baseResp;
    }

    @ApiOperation(value = "分页查询开放的景区信息",notes = "需传入 当前页page和显示行数size")
    @GetMapping("/choice/{page}/{size}")
    public BaseResp findAllByChoiceOpen(@PathVariable("page")int page,@PathVariable("size") int size){
        BaseResp places= placeService.findAllByChoiceOpen(page,size);
        return places;
    }
    @ApiOperation(value = "模糊查询 根据景点名分页全部的景区信息",notes = "需传入 当前页page和显示行数size和景点名pid")
    @GetMapping("/findByName/{page}/{size}/{placename}")
    public BaseResp findByName(@PathVariable("page")int page,@PathVariable("size") int size,@PathVariable("placename") String placename){
        BaseResp places= placeService.findByName(page,size,placename);
        return places;
    }
    @ApiOperation(value = "模糊查询 根据景点名分页查询已开放的景区信息",notes = "需传入 当前页page和显示行数size和景点名pid")
    @GetMapping("/findByNameAndOpen/{page}/{size}/{placename}")
    public BaseResp findByNameAndOpen(@PathVariable("page")int page,@PathVariable("size") int size,@PathVariable("placename") String placename){
        BaseResp places= placeService.findByNameAndOpenByPage(page,size,placename);
        return places;
    }
    @ApiOperation(value = "根据pid查看所有评论",notes = "需传入 景区pid")
    @GetMapping("/findAllBycom/{id}/{page}/{size}")
    public BaseResp findAllBycom(@PathVariable("id")int id,int page,int size){
        BaseResp places= placeService.findAllBycomByPage(id,page,size);
        return places;
    }
    @ApiOperation(value = "根据uid和pid添加评论",notes = "需传入 景区pid和用户uid和评论信息")
    @PostMapping("/addBycom")
    public BaseResp addBycom(@RequestBody Placecom placecom){
        BaseResp places= placeService.addBycom(placecom);
        return places;
    }
    @ApiOperation(value = "点击率排行榜",notes = "不用传")
    @PostMapping("/clickfirst")
    public BaseResp clickfirst(){
        BaseResp places= placeService.clickfirst();
        return places;
    }

    @ApiOperation(value = "使用七牛云上传图片存入景点图片",notes = "需传入 图片文件和景区pid")
    @PostMapping("/upload/{pid}")
    public BaseResp uploadplacepic(MultipartFile file,@PathVariable("pid") int pid) {
        String upload = UploadPic.upload(file);
        //存入数据库中
        return placeService.insertPlacePic(upload, pid);
    }

    @ApiOperation(value = "使用七牛云上传时,删除图片上传",notes = "需传入 图片文件和景区pid")
    @PostMapping("/deletepic/{pid}")
    public BaseResp deleteplacepic(@RequestBody String file,@PathVariable("pid") int pid) {
        System.out.println("file = " + file);
        return null;
    }

    @PostMapping("/sign")
    @ApiOperation(value = "实现打卡功能 获取地址(省市区路)",notes = "需传入 sign对象,里面有uid,location,date")
    public BaseResp save(@RequestBody Sign sign) {
       //判断是否打过卡
        LocalDate date = LocalDate.now(); // get the current date
        Boolean issign = placeService.issign(sign.getUid(), sign.getLocation(), date);
        if (issign){
            return new BaseResp().FAIL("你打过卡了");
        }else {
            Boolean aBoolean = placeService.addSing(sign.getUid(), sign.getLocation(), date);
            if (aBoolean){
                return new BaseResp().SUCCESS("打卡成功",null,null);
            }else {
                return new BaseResp().FAIL("打卡失败");
            }

        }
    }

    @GetMapping("/orders/{uid}/{pid}")
    @ApiOperation(value = "添加景区门票的订单",notes = "需传入 景区pid,用户uid")
    public BaseResp ordersPlace(@PathVariable("uid") int uid,@PathVariable("pid") int pid){
       Boolean ok= placeService.addOrdersPlace(uid,pid);
       if (ok)
       return new BaseResp().SUCCESS("添加成功",null,null);
        return new BaseResp().FAIL("添加失败");
    }

    @GetMapping("/upDateOrdersPlace/{uid}/{pid}")
    @ApiOperation(value = "退订景区门票的订单",notes = "需传入 景区pid,用户uid")
    public BaseResp upDateOrdersPlace(@PathVariable("uid") int uid,@PathVariable("pid") int pid){
        Boolean ok= placeService.upDateOrdersPlace(uid,pid);
        if (ok)
            return new BaseResp().SUCCESS("退订成功",null,null);
        return new BaseResp().FAIL("退订失败");
    }

    @ApiOperation(value = "去支付",notes = "需传入 订单id oid")
    @GetMapping("/pay/{oid}")
    public BaseResp pay(@PathVariable("oid") int oid,  HttpServletResponse httpServletResponse) throws IOException{
        PlaceOrder order = placeOrderMapper.findByoid(oid);
        if (order.getMoney()==0){
            return new BaseResp().FAIL("该景区不需要收费");
        }else {
            String form = new AliPay().toPay(order);
            return new BaseResp().SUCCESS("去支付",form,null);
        }
    }

    @GetMapping("/orderreturn")
    @ApiOperation(value = "支付宝回调 返回值",notes = "需传入 订单编号oid")
    public void ordereturn(HttpServletResponse httpServletResponse) throws IOException {
        httpServletResponse.setContentType( "text/html;charset=utf-8");
        String wr="<a href=\"/place/list\">成功支付</a>";
        httpServletResponse.getWriter().write(wr);
        httpServletResponse.getWriter().flush();
        httpServletResponse.getWriter().close();
    }

    @PostMapping("/orderreturn/{oid}")
    @ApiOperation(value = "支付宝回调 修改订单状态为1",notes = "需传入 订单编号oid")
    public BaseResp ordereturn2(@PathVariable("oid") int oid){
        Boolean ok=placeService.updateOrdersfortwo(oid);
        if (ok)
            return new BaseResp().SUCCESS("支付成功",null,null);
        return new BaseResp().FAIL("支付失败");
    }

    @GetMapping("/findAllOrdersok/{page}/{size}/{uid}")
    @ApiOperation(value = "查看已支付订单",notes = "需传入 页码值和用户id")
    public BaseResp findAllOrdersok(@PathVariable("uid") int uid,@PathVariable("page") int page,@PathVariable("size") int size){
        BaseResp allOrders = placeService.findAllOrdersok(page,size,uid);
        return allOrders;
    }

    @GetMapping ("/findAllOrdersno/{page}/{size}/{uid}")
    @ApiOperation(value = "查看未支付订单",notes = "需传入 页码值和用户id用户id")
    public BaseResp findAllOrdersno(@PathVariable("uid") int uid,@PathVariable("page") int page,@PathVariable("size") int size) {
        BaseResp allOrders = placeService.findAllOrdersno(page,size,uid);
        return allOrders;
    }
        //后台管理

    @PutMapping("/update")
    @ApiOperation(value = "后台管理 对于景区信息的修改",notes = "需传入 place对象")
    public BaseResp update(@RequestBody Place place){
       Boolean ok = placeService.updatePlace(place);
       if (ok){
           return new BaseResp().SUCCESS("修改成功",null,null);
       }
       return new BaseResp().FAIL("修改失败");
    }

    @PostMapping("/insert")
    @ApiOperation(value = "后台管理 对于景区信息的增加",notes = "需传入 place对象")
    public BaseResp insert(@RequestBody Place place){
        Boolean ok = placeService.insertPlace(place);
        if (ok){
            return new BaseResp().SUCCESS("新增成功",null,null);
        }
        return new BaseResp().FAIL("新增失败");
    }

    @DeleteMapping("/delete/{pid}")
    @ApiOperation(value = "后台管理 对于景区信息的删除",notes = "需传入 place对象")
    public BaseResp delete(@PathVariable int pid){
        Boolean ok = placeService.deletePlace(pid);
        if (ok){
            return new BaseResp().SUCCESS("删除成功",null,null);
        }
        return new BaseResp().FAIL("删除失败");
    }


}
